#define _CRT_SECURE_NO_WARNINGS 1

//https://leetcode.cn/problems/basic-calculator-ii/

class Solution {
public:
    int calculate(string s) {
        vector<int> stack;
        int n = s.size(), i = 0;
        char op = '+';
        while (i < n)
        {
            if (s[i] == ' ') i++;
            else if ('0' <= s[i] && s[i] <= '9')
            {
                int tmp = 0;
                while (i < n && '0' <= s[i] && s[i] <= '9')
                {
                    tmp = (tmp * 10) + (s[i] - '0');
                    i++;
                }
                if (op == '+') stack.push_back(tmp);
                else if (op == '-') stack.push_back(-tmp);
                else if (op == '*') stack.back() *= tmp;
                else stack.back() /= tmp;
            }
            else
            {
                op = s[i++];
            }
        }

        int ret = 0;
        for (auto a : stack) ret += a;
        return ret;
    }
};